/**
 * @file:          stspin32g4.h
 * @brief:
 * @details:
 * @author:        wjh
 * @date created:  2025.07.20
 * @version:       1.0.0
 * @par copyright (c):
 *
 * @par history (desc):
 *   version:1.0.0, wjh, 2025.07.20,03:30:36
 */

#ifndef _STSPIN32G4_H_
#define _STSPIN32G4_H_

/* Include Files **************************************************************/
#include "stdint.h"

/* Macro Definition ***********************************************************/
#define STSPIN32G4_I2C_ADDR (0x47 << 1)

#define STSPIN32G4_REG_ADDR_POWMNG 0x01
#define STSPIN32G4_REG_ADDR_LOGIC 0x02
#define STSPIN32G4_REG_ADDR_READY 0x07
#define STSPIN32G4_REG_ADDR_NFAULT 0x08
#define STSPIN32G4_REG_ADDR_CLEAR 0x09
#define STSPIN32G4_REG_ADDR_STBY 0x0A
#define STSPIN32G4_REG_ADDR_LOCK 0x0B
#define STSPIN32G4_REG_ADDR_RESET 0x0C
#define STSPIN32G4_REG_ADDR_STATUS 0x80

#define STSPIN32G4_I2C_DELAY_MS 500

/* Data Type Define ***********************************************************/
struct Stspin32g4Reg_POWMNG
{
    uint8_t VCC_VAL : 2;
    uint8_t RESERVE1 : 2;
    uint8_t STBY_REG_EN : 1;
    uint8_t VCC_DIS : 1;
    uint8_t REG3V3_DIS : 1;
    uint8_t RESERVE2 : 1;
};
struct Stspin32g4Reg_LOGIC
{
    uint8_t ILOCK : 1;
    uint8_t DTMIN : 1;
    uint8_t VDS_P_DFG : 2;
    uint8_t RESERVE : 4;
};
struct Stspin32g4Reg_READY
{
    uint8_t VCC_UVLO_RDY : 1;
    uint8_t THSD_RDY : 1;
    uint8_t RESERVE1 : 1;
    uint8_t STBY_RDY : 1;
    uint8_t RESERVE2 : 4;
};
struct Stspin32g4Reg_NFAULT
{
    uint8_t VCC_UVLO_FLT : 1;
    uint8_t THSD_FLT : 1;
    uint8_t VDS_P_FLT : 1;
    uint8_t RESERVE : 5;
};
struct Stspin32g4Reg_CLEAR
{
    uint8_t all;
};
struct Stspin32g4Reg_STBY
{
    uint8_t STYB : 1;
    uint8_t RESERVE : 7;
};
struct Stspin32g4Reg_LOCK
{
    uint8_t LOCK : 4;
    uint8_t NLOCK : 4;
};
struct Stspin32g4Reg_RESET
{
    uint8_t all;
};
struct Stspin32g4Reg_STATUS
{
    uint8_t VCC_UVLO : 1;
    uint8_t THSD : 1;
    uint8_t VDS_P : 1;
    uint8_t RESET : 1;
    uint8_t RESERVE : 3;
    uint8_t LOCK : 1;
};

typedef struct __attribute__((packed)) Stspin32g4Reg
{
    struct Stspin32g4Reg_POWMNG POWMNG;
    struct Stspin32g4Reg_LOGIC LOGIC;
    struct Stspin32g4Reg_READY READY;
    struct Stspin32g4Reg_NFAULT NFAULT;
    struct Stspin32g4Reg_CLEAR CLEAR;
    struct Stspin32g4Reg_STBY STBY;
    struct Stspin32g4Reg_LOCK LOCK;
    struct Stspin32g4Reg_RESET RESET;
    struct Stspin32g4Reg_STATUS STATUS;
} Stspin32g4Reg;

typedef struct __attribute__((packed)) Stspin32g4RegConfig
{
    struct Stspin32g4Reg_POWMNG POWMNG;
    struct Stspin32g4Reg_LOGIC LOGIC;
    struct Stspin32g4Reg_READY READY;
    struct Stspin32g4Reg_NFAULT NFAULT;
    // struct Stspin32g4Reg_CLEAR CLEAR;
    // struct Stspin32g4Reg_STBY STBY;
    // struct Stspin32g4Reg_LOCK LOCK;
    // struct Stspin32g4Reg_RESET RESET;
    // struct Stspin32g4Reg_STATUS STATUS;
} Stspin32g4RegConfig;

typedef struct Stspin32g4
{
    /* public data */
    uint8_t param_i2c_addr;
    Stspin32g4RegConfig param_reg_config;
    Stspin32g4Reg reg;

    /* private data */

    /* public method */
    void (*Init)(struct Stspin32g4 *self);
    void (*WriteReg)(struct Stspin32g4 *self, uint8_t reg_addr, uint8_t write_data);
    uint8_t (*ReadReg)(struct Stspin32g4 *self, uint8_t reg_addr);
    void (*ReadAllReg)(struct Stspin32g4 *self);
    void (*WriteConfig)(struct Stspin32g4 *self);
    void (*Cmd_Reset)(struct Stspin32g4 *self);
    void (*Cmd_LowConsumptionMode)(struct Stspin32g4 *self, uint8_t enter);
    void (*Cmd_FaultClear)(struct Stspin32g4 *self);
    void (*Cmd_LockControl)(struct Stspin32g4 *self, uint8_t lock);

    /* private method */

} Stspin32g4;

/* Variable Declare ***********************************************************/

/* Function Declare ***********************************************************/
void Stspin32g4_Init(struct Stspin32g4 *self);
void Stspin32g4_WriteReg(struct Stspin32g4 *self, uint8_t reg_addr, uint8_t write_data);
uint8_t Stspin32g4_ReadReg(struct Stspin32g4 *self, uint8_t reg_addr);
void Stspin32g4_ReadAllReg(struct Stspin32g4 *self);
void Stspin32g4_WriteConfig(struct Stspin32g4 *self);
void Stspin32g4_Cmd_Reset(struct Stspin32g4 *self);
void Stspin32g4_Cmd_LowConsumptionMode(struct Stspin32g4 *self, uint8_t enter);
void Stspin32g4_Cmd_FaultClear(struct Stspin32g4 *self);
void Stspin32g4_Cmd_LockControl(struct Stspin32g4 *self, uint8_t lock);

#endif